### Still need to handle bus messages for EOS and tags and what not
### Still need to handle volume in real time
### Will change the track function when kevan writes the code to access all
### the media in a directory



#!/usr/bin/python

#  This program is free software.
#  You can redistribute it and/or modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation.
#  That is either version 2 of the License, or if you prefer any later version.

import pygst
import pygtk, gtk, gobject
import gst
import sys


class Stream:
    
   def __init__(self, currDirPath, fileName, volume):
       self.fileTags = {}
       # list of filenames
       # directory remains
       # the same
       ##self._playlist
       self._currDirPath = 'file:///' + currDirPath # append with the file things
       self._fileName = fileName # set the file name 
       self._volume = volume # set volume

       self._stream = gst.element_factory_make("playbin", "stream")

       self.bus = self._stream.get_bus()
       self.bus.add_signal_watch()
       self.bus.connect("message", self.busMessage)
       self.mainloop = gobject.MainLoop()  # must use a mainloop to allow bus
                                           # messages


    # Bus message magic
   def busMessage (self, bus, message):

      mesType = message.type # get the type of message
        
      if mesType == gst.MESSAGE_EOS:
          self.stopStream()      ## method call to next song goes here :P
          sys.exit() # only to totally kill the main loop and the whole program
                     # for the moment
          print "End of Stream" 
          
      # still to stop it spittin back artist name
      # more than once

      # artist and title are the tags to listen out for
      # at the moment this is printing out serveral times
      # not sure why, think it may be to with several tags
      # containing this data
      elif mesType == gst.MESSAGE_TAG:
          taglist = message.parse_tag()
          for key in taglist.keys():
              self.fileTags[key] = taglist[key]
          print self.fileTags['artist'] + " - " + self.fileTags['title']

      elif mesType == gst.MESSAGE_ERROR:
         print "Error"


      
   
   def getcurrDirPath(self):
       print self._currDirPath # for tesing

   def getfileName(self):
       print self._fileName # for testing

   # working on this
   def gettrackLength(self):
      print self._stream.query(gst.QUERY_DURATION) 

   # change the dir
   def setcurrDirPath(self, currDirPath):
      self._currDirPath = currDirPath

   # change the file name
   # could handle the files in a list
   # when kevan writes the code
   def setfileName(self, fileName):
      self._fileName = fileName

   # set volume, yet to make this dynamic
   def setVolume(self, volume):
      self._volume = volume
      Stream._stream.remove(self.playbin)
      self.createStream()
      
   # creates the stream by adding properties to playbin
   # if the stream was to be extended with more elements
   # they wud go here as well 
   def createStream(self):
      self._stream.set_property('uri', self._currDirPath+self._fileName)
      self._stream.set_property('volume', self._volume)


   # plays the stream
   def playStream(self):
     self._stream.set_state(gst.STATE_PLAYING)
     # could call next track if tracks are in a list
     # and a catch EOF

   # pause stream
   def pauseStream(self):
      self._stream.set_state(gst.STATE_PAUSED)

   # stops stream
   def stopStream(self):
      self._stream.set_state(gst.STATE_NULL)

   # change file
   def changeTrack(self, newFile):
      self.stopStream()
      self.setfileName(newFile)
      self._stream.remove(self.playbin)# remove from stream
                                         # effectivly kills stream as it
                                         # only consits of one element anyway :P

                                         # may hit the fan if theres more than
                                         # one element i.e. wen the stream is added
      self.createStream()
      self.playStream()


   def run(self):
      self.mainloop.run()


if __name__ == "__main__":
    test = Stream('home/chief/Desktop/gStreamer/', 'music.mp3', 1)
    test.createStream()
    test.playStream()
    test.run()


